from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from django.conf import settings
import os

from alipay import AliPay
from meiduo_mall.utils.views import LoginRequiredJSONMixin
from orders.models import OrderInfo
from .models import Payment

# Create your views here.

#1、支付接口，返回支付页面url
class PaymentView(LoginRequiredJSONMixin, View):
    def get(self, request, order_id):
        #1、提取参数
        user = request.user
        #２、校验参数
        try:
            order = OrderInfo.objects.get(
                user=user, order_id=order_id
            )
        except OrderInfo.DoesNotExist as e:
            return JsonResponse({
                'code': 400,
                'errmsg': '订单不存在'
            })
        #3、业务数据处理－－通过支付宝sdk接口，获取支付页面url
        #获取支付对象
        alipay = AliPay(
            appid=settings.ALIPAY_APPID,
            app_notify_url = None,
            app_private_key_path=os.path.join(
                os.path.dirname(os.path.abspath(__file__)),
                'keys/app_private_key.pem'
            ),
            alipay_public_key_path=os.path.join(
                os.path.dirname(os.path.abspath(__file__)),
                'keys/alipay_public_key.pem'
            ),
            sign_type='RSA2',
            debug=settings.ALIPAY_DEBUG
        )
        #拼接完整的支付页面url
        order_string = alipay.api_alipay_trade_page_pay(
            subject='美多商城订单：%s' % order_id,
            out_trade_no = order_id,
            total_amount = float(order.total_amount),
            return_url = settings.ALIPAY_RETURN_URL,
            notify_url = None
        )
        alipay_url = settings.ALIPAY_URL + '?' + order_string
        #构建响应
        return JsonResponse({'code': 0, 'errmsg': 'ok', 'alipay_url': alipay_url})


#支付接口２,支付成功，验证数据，保存支付流水号和修改订单支付状态
class PaymentStatusView(LoginRequiredJSONMixin, View):
    def put(self, request):
        #提取参数
        user = request.user
        #提取支付宝参数
        query_dict = request.GET
        data = query_dict.dict()
        #提取签名－－后续根据该签名验证数据的真伪
        sign = data.pop('sign')
        #我们需要把签名从字典中取出，并从原字典中删除，
        #因为后续验证的数据支付宝接口约定，需要去除签名信息
        #校验参数
        alipay = AliPay(
            appid=settings.ALIPAY_APPID,
            app_notify_url = None,
            app_private_key_path = os.path.join(
                os.path.dirname(os.path.abspath(__file__)),
                'keys/app_private_key.pem'
            ),
            alipay_public_key_path = os.path.join(
                os.path.dirname(os.path.abspath(__file__)),
                'keys/alipay_public_key.pem'
            ),
            sign_type = 'RSA2',
            debug=settings.ALIPAY_DEBUG
        )
        if not alipay.verify(data, sign):
            return JsonResponse({'code': 400, 'errmsg': '支付失败'})
        #业务数据处理，保存支付流水号和修改订单状态
        order_id = data.get('out_trade_no')
        trade_no = data.get('trade_no')
        #修改订单状态
        order = OrderInfo.objects.get(order_id=order_id)
        order.status = OrderInfo.ORDER_STATUS_ENUM('UNSEND')
        order.save()
        #保存支付流水号
        Payment.objects.create(
            order=order,
            trade_id=trade_no
        )
        #构建响应
        return JsonResponse({'code': 0, 'errmsg': 'ok',
                             'trade_id': trade_no})
